package com.excuse.excusebuilder.presenter;

import android.content.Context;

import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.commen.util.ToastUtil;
import com.excuse.excusebuilder.impl.callback.CommunityCallBack;
import com.excuse.excusebuilder.impl.model.CommunityModelInterface;
import com.excuse.excusebuilder.impl.view.CommunityFragmentInterface;
import com.excuse.excusebuilder.model.CommunityModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U-nookia on 2017/4/18.
 */

public class CommunityFramentPresenter implements CommunityCallBack{
    private Context context;
    private CommunityFragmentInterface communityFragment;
    private CommunityModelInterface communityModel;
    private List<Post> mPosts;

    public CommunityFramentPresenter(Context context, CommunityFragmentInterface communityFragment) {
        this.context = context;
        this.communityFragment = communityFragment;
        communityModel = new CommunityModel();
        mPosts = new ArrayList<>();
    }

    /*
    获取所有帖子
     */
    public void getPosts() {
        if (!NetWorkUtil.netWorkConnected){
            ToastUtil.makeShortToast(context,"请检查网络");
            return;
        }
        communityModel.getPostsFromBmob(this);
    }

    @Override
    public void hasError(String error) {
        ToastUtil.makeShortToast(context,error);
    }

    @Override
    public void getPosts(List<Post> posts) {
        mPosts = posts;
        communityFragment.setAdapter(mPosts);
    }
}
